home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
pcr
/
pcr4_4.lha
/
DIST
/
debugnub
/
CirioNubStoreBytesToText.c
< prev
next >
Wrap
C/C++ Source or Header
|
1991-06-17
|
5KB
|
181 lines
/* begincopyright
Copyright (c) 1991 Xerox Corporation. All rights reserved.
Use and copying of this software and preparation of derivative works based
upon this software are permitted. Any distribution of this software or
derivative works must comply with all applicable United States export
control laws. This software is made available AS IS, and Xerox Corporation
makes no warranty about the software, its performance or its conformity to
any specification. Any person obtaining a copy of this software is requested
to send their name and post office or electronic mail address to:
PCR Coordinator
Xerox PARC
3333 Coyote Hill Rd.
Palo Alto, CA 94304
endcopyright */
/*
* CirioNubStoreBytesToText.c
*
* Demers, June 6, 1991 2:49:24 pm PDT
*/
/*
NOTE: This doesn't need to use ptrace -- it could instead
issue vp orders to get each vp to write-enable that part
of the text segment it needs to write in. The semantics
would still be copy-on-write, the code would be somewhat
more complex. The result would work on SVR4, but not in AIX.
So much for standards.
*/
#include "xr/Errno.h"
#include "xr/ThreadsBackdoor.h"
#include "xr/CirioNubProtocol.h"
#include "xr/CirioNubMarshall.h"
#include "xr/CirioNubProcs.h"
#include "xr/CirioNubEnvironment.h"
#include "xr/ThreadsMsg.h"
#include <signal.h>
#include <sys/ptrace.h>
#include <sys/wait.h>
#define DEBUG_STORE 1
#define DEBUG_FETCH 1
#undef DEBUG_FETCH
extern int etext;
#define TEXTSTART ((char *)(8*1024))
#define TEXTLIM ((char *)(&etext))
bool
CirioNubIsTextAddress(addr)
char *addr;
{
if( addr < TEXTSTART ) return FALSE;
if( addr >= TEXTLIM) return FALSE;
return TRUE;
}
CirioNubRetCode
CirioNubStoreBytesToVPTextSegments(bytes, addr, nBytes)
char *bytes;
char *addr;
int nBytes;
{
int ans, status, i;
int pid = 0;
if( !CirioNubIsTextAddress(addr) ) {
return CirioNubPutInt32(-1);
}
for( i = 0; i < XR_sysArea->sa_numVP; i++ ) {
pid = XR_sysArea->sa_vpe[i].vpe_pid;
ans = ptrace(PTRACE_ATTACH, pid, 0, 0, 0);
if( ans == (-1) ) {
# ifdef DEBUG_STORE
XR_ConsoleMsg("Can't attach pid %d err %d\n", pid, errno);
# endif
pid = 0; goto Bad;
}
for(;;) {
ans = waitpid(pid, &status, WUNTRACED);
if( ans != (-1) ) break;
if( errno == ECHILD ) {
# ifdef DEBUG_STORE
XR_ConsoleMsg("Can't wait pid %d err %d\n", pid, errno);
# endif
goto Bad;
}
}
ans = ptrace(PTRACE_WRITETEXT, pid, addr, nBytes, bytes);
if( ans == (-1) ) {
# ifdef DEBUG_STORE
XR_ConsoleMsg("Can't write pid %d err %d\n", pid, errno);
# endif
goto Bad;
}
ans = ptrace(PTRACE_DETACH, pid, ((int *)(1)), SIGUSR1, 0);
if( ans == (-1) ) {
# ifdef DEBUG_STORE
XR_ConsoleMsg("Can't detach pid %d err %d\n", pid, errno);
# endif
pid = 0; goto Bad;
}
}
return CirioNubPutInt32(0);
Bad:
if( pid != 0 ) {
(void)ptrace(PTRACE_DETACH, pid, ((int *)(1)), SIGUSR1, 0);
}
return CirioNubPutInt32(-1);
}
int
CirioNubFetchBytesFromVPTextSegment(bytes, addr, nBytes, vpi)
char *bytes;
char *addr;
int nBytes;
int vpi;
{
int ans, status;
int pid;
if( !CirioNubIsTextAddress(addr) ) {
return (-1);
}
{
pid = XR_sysArea->sa_vpe[vpi].vpe_pid;
ans = ptrace(PTRACE_ATTACH, pid, 0, 0, 0);
if( ans == (-1) ) {
# ifdef DEBUG_FETCH
XR_ConsoleMsg("Can't attach pid %d err %d\n", pid, errno);
# endif
pid = 0; goto Bad;
}
for(;;) {
ans = waitpid(pid, &status, WUNTRACED);
if( ans != (-1) ) break;
if( errno == ECHILD ) {
# ifdef DEBUG_FETCH
XR_ConsoleMsg("Can't wait pid %d err %d\n", pid, errno);
# endif
goto Bad;
}
}
ans = ptrace(PTRACE_READTEXT, pid, addr, nBytes, bytes);
if( ans == (-1) ) {
# ifdef DEBUG_FETCH
XR_ConsoleMsg("Can't read pid %d err %d\n", pid, errno);
# endif
goto Bad;
}
ans = ptrace(PTRACE_DETACH, pid, ((int *)(1)), SIGUSR1, 0);
if( ans == (-1) ) {
# ifdef DEBUG_FETCH
XR_ConsoleMsg("Can't detach pid %d err %d\n", pid, errno);
# endif
pid = 0; goto Bad;
}
}
return 0;
Bad:
if( pid != 0 ) {
(void) ptrace(PTRACE_DETACH, pid, ((int *)(1)), SIGUSR1, 0);
}
return (-1);
}